<?php

/** @property CI_Migration $migration */
class install extends MY_Controller
{
    public function index()
    {
        //判断是否已安装
        if ($this->db->table_exists('option') && Option_Model::getOption('installed'))
            show_404();

        $siteName     = $this->input->post('site-name');
        $userEmail    = $this->input->post('user-email');
        $userPassword = $this->input->post('user-password');

        if (!$siteName || !$userEmail || !$userPassword) {
            $this->load->view('install');
        } else {

            // 数据库迁移
            $this->load->library('migration');
            if (!$this->migration->current()) {
                show_error($this->migration->error_string());
            }

            // 设置基础数据
            Option_Model::updateOption('installed', true);
            Option_Model::updateOption('site_name', $siteName);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('手机数码');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/1.png');
            $thatFatherCommodityCategory = CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('手机');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/phone.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('相机');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/camera.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('电脑周边');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/2.png');
            $thatFatherCommodityCategory = CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('电脑');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/Computer.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('电脑外设');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/mouse.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('硬件');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/hard.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('生活电器');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/3.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('日常生活');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/4.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('美容保健');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/5.png');
            $thatFatherCommodityCategory = CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('护肤品');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/mask.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('保健品');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/heart.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('书籍文具');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/6.png');
            $thatFatherCommodityCategory = CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('书籍');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/book.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('学习办公用品');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/radio.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('娱乐运动');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/7.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('工艺饰品');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/8.png');
            $thatFatherCommodityCategory = CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('手工制作');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/hand.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('时尚饰品');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/ring.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('手表眼镜');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/wristwatch.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('时尚服饰');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/9.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('箱包鞋靴');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/10.png');
            $thatFatherCommodityCategory = CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('箱包');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/briefcase.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('鞋靴');
            $thatCommodityCategory->setFatherId($thatFatherCommodityCategory->getId());
            $thatCommodityCategory->setIconUrl('public/images/classify/shoe.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('食品饮料');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/11.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------------------------------------------------------------------------

            $thatCommodityCategory = new CommodityCategory();
            $thatCommodityCategory->setName('其他物品');
            $thatCommodityCategory->setFatherId(0);
            $thatCommodityCategory->setIconUrl('public/images/classify/12.png');
            CommodityCategory_Model::add($thatCommodityCategory);

            //-----------------------------Tags数据---------------------------------------------------------------------

            $tagsData = array(
                '洗衣机',
                '手机',
                '笔记本',
                '电脑',
                '书',
                '自行车'
            );
            foreach($tagsData as $tagName){
                 $this->db->insert('commodity_tag', array('name'=>$tagName));
            }



            //-----------------------------学校数据---------------------------------------------------------------------
//            广州大学华软软件学院	12618
//            中山大学南方学院	12619
//            广东财经大学	10592
//            华南农业大学珠江学院	12623
//            广州大学	11078
//            广东水利电力职业技术学院	24407

            $schoolList=array(
                array("广州大学华软软件学院",12618,1),
                array("中山大学南方学院",12619,2),
                array("广东财经大学",10592,3),
                array("华南农业大学珠江学院",12623,4),
                array("广州大学",11078,5),
                array("广东水利电力职业技术学院",24407,6),
            );

            foreach($schoolList as $s){
                $school=new School();
                $school->setName($s[0]);
                $school->setUnicode($s[1]);
                $school->setAuthType(1);
                School_Model::add($school);
            }

            // 注册初始管理员
            $thatUser = UserManager::register($userEmail, $userPassword);
            $thatUser->setPower(99999);
            User_Model::update($thatUser);

            $this->load->view('install', array('isSuccess' => true));
    }
    }
} 